<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="发票信息" prop="exampleOne">
        <el-input
          v-model="queryParams.exampleOne"
          placeholder="请输入交易方信息"
          clearable
        />
      </el-form-item>
      <el-form-item label="发票金额" prop="preInvoiceAmount">
        <el-input
          v-model="queryParams.preInvoiceAmount"
          placeholder="请输入发票金额"
          clearable
        />
      </el-form-item>
      <el-form-item label="开票时间" prop="issueTime">
        <el-date-picker
          v-model="queryParams.issueTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          clearable
        />
      </el-form-item>
      <el-form-item label="关联状态" prop="preInvoiceStatus">
        <el-select
          v-model="queryParams.preInvoiceStatus"
          placeholder="请选择关联状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.pre_invoice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          @click="handleExport"
          v-hasPermi="['training:preInvoice:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          @click="handleAdd"
          v-hasPermi="['training:preInvoice:add']"
          >预开票</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          :disabled="single"
          @click="handleEditInvoiceInfo"
          >编辑预开票</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain :disabled="single" @click="handleChange"
          >退换票</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          :disabled="!selectedRow"
          @click="handleCorrelate(selectedRow)"
          >关联发票</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          :disabled="!selectedRow"
          @click="handleView"
          v-hasPermi="['training:preInvoice:view']"
          >查看发票</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          :disabled="!selectedRow"
          @click="handleDelete"
          v-hasPermi="['training:preInvoice:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- 右侧浮动按钮组 -->
      <el-col :span="6" style="float: right; margin-top: 0">
        <el-button-group>
          <el-button type="primary" plain @click="toggleSearch"
            >隐藏搜索</el-button
          >
          <el-button type="primary" plain @click="refreshTable">刷新</el-button>
          <el-button type="primary" plain @click="toggleSort"
            >隐藏/排序列</el-button
          >
        </el-button-group>
      </el-col>
    </el-row>

    <!-- 按钮组件 -->
    <PreInvoiceForm
      ref="preInvoiceForm"
      title="预开票信息"
      @submit="onSubmitPreInvoice"
    />
    <CorrelateInvoiceDialog
      ref="correlateInvoiceDialog"
      :title="'关联发票'"
      :dict="dictCache"
      :invoice-data="selectedRow"
      @confirm="handleCorrelateSubmit"
    />
    <PreInvoiceRelationDialog
      ref="preInvoiceRelationDialog"
      title="预开票关联报名信息"
      :dict="dictCache"
      @confirm="handleRelationConfirm"
    />
    <RefundInvoiceDialog
      ref="refundInvoiceDialog"
      :visible.sync="refundVisible"
      :data="selectedRow"
      :dict="dictCache"
      @submit="handleRefundSubmit"
    />
    <EditInvoiceInfoDialog
      :visible="showEditInvoiceInfo"
      :formData="invoiceFormData"
      :dict="dictCache"
      @submit="handleInvoiceSubmit"
      @close="handleEditInvoiceClose"
    ></EditInvoiceInfoDialog>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="preInvoiceList"
      @selection-change="handleSelectionChange"
      @current-change="handleCurrentChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" align="center" />

      <!-- 发票种类 -->
      <el-table-column
        label="发票种类"
        align="center"
        prop="preInvoiceCategory"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.invoice_category"
            :value="scope.row.preInvoiceCategory"
          />
        </template>
      </el-table-column>
      <!-- 发票种类 -->
      <el-table-column
        v-if="columnSettings.preInvoiceCategory.visible"
        label="发票种类"
        align="center"
        prop="preInvoiceCategory"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.invoice_category"
            :value="scope.row.preInvoiceCategory"
          />
        </template>
      </el-table-column>

      <!-- 发票类型 -->
      <el-table-column
        v-if="columnSettings.preInvoiceType.visible"
        label="发票类型"
        align="center"
        prop="preInvoiceType"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.invoice_type"
            :value="scope.row.preInvoiceType"
          />
        </template>
      </el-table-column>

      <!-- 单位名称 -->
      <el-table-column
        v-if="columnSettings.preUnitName.visible"
        label="单位名称"
        align="center"
        prop="preUnitName"
      />

      <!-- 单位统一信用代码 -->
      <el-table-column
        v-if="columnSettings.preCreditCode.visible"
        label="单位统一信用代码"
        align="center"
        prop="preCreditCode"
      />

      <!-- 联系人 -->
      <el-table-column
        v-if="columnSettings.preContact.visible"
        label="联系人"
        align="center"
        prop="preContact"
      />

      <!-- 联系人电话 -->
      <el-table-column
        v-if="columnSettings.prePhone.visible"
        label="联系人电话"
        align="center"
        prop="prePhone"
      />

      <!-- 开票时间 -->
      <el-table-column
        v-if="columnSettings.createTime.visible"
        label="开票时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>

      <!-- 开票金额 -->
      <el-table-column
        v-if="columnSettings.preInvoiceAmount.visible"
        label="开票金额"
        align="center"
        prop="preInvoiceAmount"
      />

      <!-- 关联信息 -->
      <el-table-column
        v-if="columnSettings.preCorrelation.visible"
        label="关联信息"
        align="center"
        prop="preCorrelation"
      />

      <!-- 关联金额 -->
      <el-table-column
        v-if="columnSettings.preCorrelationAmount.visible"
        label="关联金额"
        align="center"
        prop="preCorrelationAmount"
      />

      <!-- 未关联金额 -->
      <el-table-column
        v-if="columnSettings.preUnassociated.visible"
        label="未关联金额"
        align="center"
        prop="preUnassociated"
      />

      <!-- 操作列 -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="handleCorrelate(scope.row)"
            >关联发票</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预开票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报名id" prop="registrationId">
          <el-input v-model="form.registrationId" placeholder="请输入报名id" />
        </el-form-item>
        <el-form-item label="发票ID" prop="invoiceId">
          <el-input v-model="form.invoiceId" placeholder="请输入发票ID" />
        </el-form-item>
        <el-form-item label="发票种类" prop="preInvoiceCategory">
          <el-select
            v-model="form.preInvoiceCategory"
            placeholder="请选择发票种类"
          >
            <el-option
              v-for="dict in dict.type.invoice_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票类型" prop="preInvoiceType">
          <el-select v-model="form.preInvoiceType" placeholder="请选择发票类型">
            <el-option
              v-for="dict in dict.type.invoice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票状态" prop="preInvoiceStatus">
          <el-select
            v-model="form.preInvoiceStatus"
            placeholder="请选择发票状态"
          >
            <el-option
              v-for="dict in dict.type.invoice_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票抬头" prop="preInvoiceTitle">
          <el-input
            v-model="form.preInvoiceTitle"
            placeholder="请输入发票抬头"
          />
        </el-form-item>
        <el-form-item label="发票编号" prop="preInvoiceNumber">
          <el-input
            v-model="form.preInvoiceNumber"
            placeholder="请输入发票编号"
          />
        </el-form-item>
        <el-form-item label="发票金额" prop="preInvoiceAmount">
          <el-input
            v-model="form.preInvoiceAmount"
            placeholder="请输入发票金额"
          />
        </el-form-item>
        <el-form-item label="单位名称" prop="preUnitName">
          <el-input v-model="form.preUnitName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="单位统一信用代码" prop="preCreditCode">
          <el-input
            v-model="form.preCreditCode"
            placeholder="请输入单位统一信用代码"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="preContact">
          <el-input v-model="form.preContact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="prePhone">
          <el-input v-model="form.prePhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="开票时间" prop="preIssueTime">
          <el-date-picker
            clearable
            v-model="form.createTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开票时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发票信息" prop="preInvoiceImage">
          <image-upload v-model="form.preInvoiceImage" />
        </el-form-item>
        <el-form-item label="备注信息" prop="preRemark">
          <el-input v-model="form.preRemark" placeholder="请输入备注信息" />
        </el-form-item>
        <el-form-item label="关联信息" prop="preCorrelation">
          <el-input
            v-model="form.preCorrelation"
            placeholder="请输入关联信息"
          />
        </el-form-item>
        <el-form-item label="关联金额" prop="preCorrelationAmount">
          <el-input
            v-model="form.preCorrelationAmount"
            placeholder="请输入关联金额"
          />
        </el-form-item>
        <el-form-item label="未关联金额" prop="preUnassociated">
          <el-input
            v-model="form.preUnassociated"
            placeholder="请输入未关联金额"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 列设置对话框 -->
    <el-dialog
      title="列设置"
      :visible.sync="showColumnSettings"
      width="400px"
      append-to-body
    >
      <el-checkbox-group v-model="visibleColumns">
        <div
          v-for="(column, key) in columnSettings"
          :key="key"
          style="margin: 10px 0"
        >
          <el-checkbox :label="key">{{ column.label }}</el-checkbox>
        </div>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showColumnSettings = false">取消</el-button>
        <el-button type="primary" @click="saveColumnSettings">确定</el-button>
      </div>
    </el-dialog>

    
  </div>
</template>

<script>
import {
  listPreInvoice,
  getPreInvoice,
  delPreInvoice,
  addPreInvoice,
  updatePreInvoice,
  getList,
  associationRegistration,
  getRefundInfo,
  getCorrelationInfo,
} from "@/api/payment/preInvoice";
import { updateInvoice, getInvoiceRefundInfo } from "@/api/payment/invoice";
//预开票
import PreInvoiceForm from "@/components/PreInvoice/PreInvoiceForm.vue";
//关联发票
import CorrelateInvoiceDialog from "@/components/PreInvoice/CorrelateInvoiceDialog.vue";
//预开票关联报名信息
import PreInvoiceRelationDialog from "@/components/PreInvoice/PreInvoiceRelationDialog.vue";
//退换票
import RefundInvoiceDialog from "@/components/PreInvoice/RefundInvoiceDialog.vue";
// 编辑开票信息
import EditInvoiceInfoDialog from "@/components/Payment/EditInvoiceInfoDialog.vue";

export default {
  name: "PreInvoice",
  dicts: [
    "invoice_category",
    "invoice_status",
    "invoice_type",
    "payment_status",
    "pre_invoice_status",
    "registration_method",
  ],
  components: {
    PreInvoiceForm,
    CorrelateInvoiceDialog,
    PreInvoiceRelationDialog,
    RefundInvoiceDialog,
    EditInvoiceInfoDialog,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预开票表格数据
      preInvoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        preInvoiceCategory: null,
        preInvoiceType: null,
        preInvoiceStatus: null,
        preIssueTime: null,
        preInvoiceAmount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      selectedRow: null, // 当前选中的预开票行
      //退换票
      refundVisible: false, // 控制退换票对话框显示
      selectedRow: null, // 当前选中的预开票行
      // 编辑开票信息
      showEditInvoiceInfo: false,
      invoiceFormData: {
        invoiceType: "",
        invoiceCategory: "",
        invoiceUnitName: "",
        invoiceCreditCode: "",
      },
      // 缓存字典数据
      dictCache: {
        payment_method: {},
        is_settled: {},
        payment_status: {},
        invoice_status: {},
        invoice_type: {},
        invoice_category: {},
        registration_method: {},
        training_type: {},
      },

      // 列显示控制
      columnSettings: {
        preInvoiceCategory: { visible: true, label: "发票种类" },
        preInvoiceType: { visible: true, label: "发票类型" },
        preUnitName: { visible: true, label: "单位名称" },
        preCreditCode: { visible: true, label: "单位统一信用代码" },
        preContact: { visible: true, label: "联系人" },
        prePhone: { visible: true, label: "联系人电话" },
        createTime: { visible: true, label: "开票时间" },
        preInvoiceAmount: { visible: true, label: "开票金额" },
        preCorrelation: { visible: true, label: "关联信息" },
        preCorrelationAmount: { visible: true, label: "关联金额" },
        preUnassociated: { visible: true, label: "未关联金额" },
      },
      showColumnSettings: false, // 控制列设置弹窗显示
    };
  },
  created() {
    this.getList();
    this.loadColumnSettings(); // 加载列设置
    this.$nextTick(async () => {
      await this.loadDicts();
    });
  },
  computed: {
    visibleColumns: {
      get() {
        return Object.keys(this.columnSettings).filter(
          (key) => this.columnSettings[key].visible
        );
      },
      set(value) {
        Object.keys(this.columnSettings).forEach((key) => {
          this.columnSettings[key].visible = value.includes(key);
        });
      },
    },
  },
  methods: {
    async loadDicts() {
      this.dictCache.payment_status = this.dict.label.payment_status;
      this.dictCache.invoice_status = this.dict.label.invoice_status;
      this.dictCache.invoice_type = this.dict.label.invoice_type;
      this.dictCache.invoice_category = this.dict.label.invoice_category;
      this.dictCache.registration_method = this.dict.label.registration_method;
      this.dictCache.pre_invoice_status = this.dict.label.pre_invoice_status;
    },
    /** 查询预开票列表 */
    getList() {
      this.loading = true;
      getList(this.queryParams).then((response) => {
        this.preInvoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        preInvoiceId: null,
        registrationId: null,
        invoiceId: null,
        preInvoiceCategory: null,
        preInvoiceType: null,
        preInvoiceStatus: null,
        preInvoiceTitle: null,
        preInvoiceNumber: null,
        preInvoiceAmount: null,
        preUnitName: null,
        preCreditCode: null,
        preContact: null,
        prePhone: null,
        preIssueTime: null,
        preInvoiceImage: null,
        preRemark: null,
        preCorrelation: null,
        preCorrelationAmount: null,
        preUnassociated: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        exampleOne: null,
        exampleTwo: null,
        exampleThree: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // 处理日期范围字段
      if (
        this.queryParams.issueTime &&
        this.queryParams.issueTime.length === 2
      ) {
        this.queryParams.preIssueStartTime = this.queryParams.issueTime[0];
        this.queryParams.preIssueEndTime = this.queryParams.issueTime[1];
      } else {
        this.queryParams.preIssueStartTime = null;
        this.queryParams.preIssueEndTime = null;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      // 清理日期相关字段
      this.queryParams.preIssueStartTime = null;
      this.queryParams.preIssueEndTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.preInvoiceId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;

      // 当有选择项时，设置selectedRow为第一个选择项
      if (selection.length > 0) {
        this.selectedRow = selection[0];
      } else {
        this.selectedRow = null;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.preInvoiceForm.open();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const selectedRow =
        row ||
        this.preInvoiceList.find((item) => item.preInvoiceId === this.ids[0]);

      if (selectedRow) {
        // 设置表单数据
        this.invoiceFormData = {
          invoiceType: selectedRow.preInvoiceType || "",
          invoiceCategory: selectedRow.preInvoiceCategory || "",
          invoiceUnitName: selectedRow.preUnitName || "",
          invoiceCreditCode: selectedRow.preCreditCode || "",
        };
        // 显示对话框
        this.showEditInvoiceInfo = true;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.preInvoiceId != null) {
            updatePreInvoice(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPreInvoice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const preInvoiceIds = row.preInvoiceId || this.ids;
      this.$modal
        .confirm('是否确认删除预开票编号为"' + preInvoiceIds + '"的数据项？')
        .then(function () {
          return delPreInvoice(preInvoiceIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "training/preInvoice/export",
        {
          ...this.queryParams,
        },
        `preInvoice_${new Date().getTime()}.xlsx`
      );
    },

    //新增方法
    //刷新按钮组
    toggleSearch() {
      this.showSearch = !this.showSearch;
    },
    refreshTable() {
      this.getList();
    },
    toggleSort() {
      // 实现排序列切换逻辑
      this.showColumnSettings = true;
    },
    saveColumnSettings() {
      this.showColumnSettings = false;
      // 保存设置到本地存储（可选）
      localStorage.setItem(
        "preInvoiceColumnSettings",
        JSON.stringify(this.columnSettings)
      );
    },

    // 从本地存储加载列设置（可选）
    loadColumnSettings() {
      const savedSettings = localStorage.getItem("preInvoiceColumnSettings");
      if (savedSettings) {
        this.columnSettings = JSON.parse(savedSettings);
      }
    },
    //预开票提交
    onSubmitPreInvoice(data) {
      this.$nextTick(() => {
        addPreInvoice(data).then(() => {
          this.$modal.msgSuccess("预开票成功");
          this.getList(); // 刷新列表
        });
      });
    },
    handleCorrelate(row) {
      this.selectedRow = row;

      // 调用 getCorrelationInfo 获取已关联的报名信息
      if (row && row.preInvoiceId) {
        getCorrelationInfo(row.preInvoiceId)
          .then((response) => {
            console.log("已关联的报名信息:", response.data);

            if (response.data && response.data.registrationDtoList) {
              this.selectedRow.correlationData =
                response.data.registrationDtoList;
            }
            // 打开关联发票对话框
            this.$refs.correlateInvoiceDialog.open();
          })
          .catch((error) => {
            console.error("获取关联信息失败:", error);
            // 即使获取失败也打开对话框
            this.$refs.correlateInvoiceDialog.open();
          });
      } else {
        this.$refs.correlateInvoiceDialog.open();
      }
    },
    //关联发票
    handleRelationConfirm(selectedRows) {
      // 将选中的报名信息回传给 CorrelateInvoiceDialog
      this.$refs.correlateInvoiceDialog.handleConfirm(selectedRows);
    },
    //关联报名信息
    handleCorrelateSubmit(data) {
      const registrationIds = data.relationList
        .map((item) => item.registrationId)
        .join(",");
      const id = this.selectedRow.preInvoiceId;

      var params = {
        preInvoiceId: id,
        registrationId: registrationIds,
      };
      associationRegistration(params).then((response) => {
        this.$modal.msgSuccess("发票关联成功");
        this.getList(); // 刷新列表
      });
    },
    handleCurrentChange(currentRow) {
      this.selectedRow = currentRow;
    },
    /** 查看发票按钮操作 */
    handleView(row) {
      // 可以根据需求实现查看功能
      // 例如:
      // this.reset();
      // const preInvoiceId = row.preInvoiceId;
      // getPreInvoice(preInvoiceId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "查看预开票";
      // });
    },
    /** 退换票按钮操作 */
    handleChange(row) {
      // 获取选中的记录
      const selectedData = this.preInvoiceList.find(
        (item) => item.preInvoiceId === this.ids[0]
      );

      getRefundInfo(selectedData.preInvoiceId).then((response) => {
        this.selectedRow = response.data;
        this.refundVisible = true;
      });
    },

    /** 处理退换票提交 */
    handleRefundSubmit(data) {
      console.log("退换票申请数据:", data);
      updatePreInvoice(data).then(() => {
        this.$modal.msgSuccess("退换票申请成功");
        this.getList(); // 刷新列表
      });
    },
    //编辑开票信息
    // 编辑开票信息关闭处理
    handleEditInvoiceClose() {
      this.showEditInvoiceInfo = false;
    },
    // 编辑开票----提交
    handleInvoiceSubmit(formData) {
      const selectedRow = this.preInvoiceList.find(
        (item) => item.preInvoiceId === this.ids[0]
      );
      var data = {
        preInvoiceId: selectedRow.preInvoiceId,
        preUnitName: formData.invoiceUnitName,
        preCreditCode: formData.invoiceCreditCode,
        preInvoiceType: formData.invoiceType,
        preInvoiceCategory: formData.invoiceCategory,
      };
      updatePreInvoice(data).then(() => {
        this.$modal.msgSuccess("开票信息已更新");
        this.showEditInvoiceInfo = false;
        this.getList(); // 刷新列表
      });
    },
    // 编辑开票-----点击
    handleEditInvoiceInfo() {
      // 实现编辑开票信息逻辑

      const selectedRow = this.preInvoiceList.find(
        (item) => item.preInvoiceId === this.ids[0]
      );

      console.log("选中的记录:", selectedRow);

      if (selectedRow) {
        // 设置表单数据
        this.invoiceFormData = {
          invoiceType: selectedRow.preInvoiceType || "",
          invoiceCategory: selectedRow.preInvoiceCategory || "",
          invoiceUnitName: selectedRow.preUnitName || "",
          invoiceCreditCode: selectedRow.preCreditCode || "",
        };
        // 显示对话框
        this.showEditInvoiceInfo = true;
      } else {
        // 如果没有选中任何记录，初始化为空数据
        this.invoiceFormData = {
          invoiceUnitName: "",
          invoiceCreditCode: "",
          invoiceType: "1",
          invoiceCategory: "1",
        };
        this.showEditInvoiceInfo = true;
      }
    },
  },
};
</script>
